﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using YY.Basic;
using TK3DSim;
using Tpro6Container.Mil;

using Infragistics.Win.UltraWinTree;
using TK3DSim.Mil;

namespace Tpro6Container
{
    public partial class FormArmyActionBH : Form
    {
        private bool modify;
        public FormArmyActionBH()
        {
            InitializeComponent();
            Init();
            modify = false;
        }

        private Image _Img = ResourceHelper.LoadImage("Tpro6Container.Resource.target.png");

        private void Init()
        {
            UltraTreeNode oNode = null;
            ArmyActionBH oArm = null;

            for (int i = 0; i < Global.Instance.GlobalArmyActionBHs.Count; i++)
            {
                oArm = Global.Instance.GlobalArmyActionBHs[i];

                oNode = new UltraTreeNode();
                oNode.Key = "p" + oArm.ID;
                oNode.Text = oArm.Organization.ToString() + oArm.ActionName;
                oNode.Tag = oArm;
                oNode.LeftImages.Add(_Img);

                this.treeList.Nodes.Add(oNode);
            }

            this.treeList.HideSelection = false;

            this.comOrg.Items.Add((Organizations)1, Organizations.师.ToString());
            this.comOrg.Items.Add((Organizations)2, Organizations.旅.ToString());
            this.comOrg.Items.Add((Organizations)3, Organizations.团.ToString());
            this.comOrg.Items.Add((Organizations)4, Organizations.营.ToString());
            this.comOrg.Items.Add((Organizations)5, Organizations.连.ToString());
            this.comOrg.Items.Add((Organizations)6, Organizations.排.ToString());
            this.comOrg.Items.Add((Organizations)7, Organizations.蓝师.ToString());
            this.comOrg.Items.Add((Organizations)8, Organizations.蓝旅.ToString());
            this.comOrg.Items.Add((Organizations)9, Organizations.蓝团.ToString());
            this.comOrg.Items.Add((Organizations)10, Organizations.蓝营.ToString());
            this.comOrg.Items.Add((Organizations)11, Organizations.蓝连.ToString());
            this.comOrg.Items.Add((Organizations)12, Organizations.蓝排.ToString());

            this.comOrg.SelectedIndex = 0;
            if (this.treeList.Nodes.Count  >  0)
            {
                this.treeList.Nodes[0].Selected = true;
            }

        }

        private void ultraLabel18_Click(object sender, EventArgs e)
        {

        }

        private void ultraButton5_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Title = "打开标号文件";
            op.InitialDirectory = Application.StartupPath + @"\junbiao\";
            if (op.ShowDialog() == DialogResult.OK)
            {
                modify = true;
                this.txtModelName.Text = op.FileName.Remove(0, op.InitialDirectory.Length);
            }
        }

        private void bAdd_Click(object sender, EventArgs e)
        {
            int i = 1;
            while (Global.Instance.GlobalArmyActionBHs.HaveExist((Organizations)(int)Organizations.师 + i, "行进"))
            {
                i++;
            }

            UltraTreeNode oNode = null;
            ArmyActionBH oArm = null;

            oArm = Global.Instance.GlobalArmyActionBHs.Add( (Organizations)(int)Organizations.师 +  i, "行进");
            oNode = new UltraTreeNode();
            oNode.Key = "p" + oArm.ID;
            oNode.Text = oArm.Organization.ToString() + oArm.ActionName;
            oNode.Tag = oArm; ;
            oNode.LeftImages.Add(_Img);

            this.treeList.Nodes.Add(oNode);
            oNode.Selected = true;
            Global.Instance.GlobalArmyActionBHs.UpdateData();
        }

        private void bDel_Click(object sender, EventArgs e)
        {
            if (this.treeList.SelectedNodes.Count <= 0)
            {
                return;
            }
            Global.Instance.GlobalArmyActionBHs.Delete(this.treeList.SelectedNodes[0].Key.Substring(1));
            this.treeList.SelectedNodes[0].Remove();
            Global.Instance.GlobalArmyActionBHs.UpdateData();
        }

        private void bCopy_Click(object sender, EventArgs e)
        {
            if (this.treeList.SelectedNodes.Count <= 0)
            {
                return;
            }

            if (this.txtName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("名称不能为空");
                txtName.Focus();
                return;
            }
            else
            {
                if (this.treeList.SelectedNodes[0].Text != txtName.Text.Trim())
                {
                    if (Global.Instance.GlobalArmaments.HaveExist(this.txtName.Text.Trim()))
                    {
                        MessageBox.Show("名称已经存在");

                        txtName.Focus();
                        return;
                    }
                }
            }

            UltraTreeNode oNode = null;
            ArmyActionBH oArm = null;

            oArm = Global.Instance.GlobalArmyActionBHs.Copy(this.treeList.SelectedNodes[0].Key.Substring(1));
            oNode = new UltraTreeNode();
            oNode.Key = "p" + oArm.ID;
            oNode.Text = oArm.Organization.ToString() + oArm.ActionName;
            oNode.Tag = oArm; 
            oNode.LeftImages.Add(_Img);

            this.treeList.Nodes.Add(oNode);
            oNode.Selected = true;
            Global.Instance.GlobalArmyActionBHs.UpdateData();
        }

        private void bSave_Click(object sender, EventArgs e)
        {
            if (this.treeList.SelectedNodes.Count <= 0)
            {
                return;
            }

            if (this.txtName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("名称不能为空");
                txtName.Focus();
                return;
            }
            else
            {
                if (this.treeList.SelectedNodes[0].Text != txtName.Text.Trim())
                {
                    if (Global.Instance.GlobalArmaments.HaveExist(this.txtName.Text.Trim()))
                    {
                        MessageBox.Show("名称已经存在");
                        txtName.Focus();
                        return;
                    }
                }
                bool b = Global.Instance.GlobalArmyActionBHs.UpdateData();

                WriteBackTo((ArmyActionBH)this.treeList.SelectedNodes[0].Tag);
                this.treeList.SelectedNodes[0].Text = ((ArmyActionBH)this.treeList.SelectedNodes[0].Tag).Organization.ToString() + ((ArmyActionBH)this.treeList.SelectedNodes[0].Tag).ActionName;
                if (b == true)
                {
                    MessageBox.Show("您的修改已保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show("*您的修改保存失败!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void treeList_BeforeSelect(object sender, BeforeSelectEventArgs e)
        {
            if (this.treeList.SelectedNodes.Count <= 0)
            {
                return;
            }

            if (this.txtName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("名称不能为空");
                e.Cancel = true;
                txtName.Focus();
            }
            else
            {
                if (this.treeList.SelectedNodes[0].Text != txtName.Text.Trim())
                {
                    if (Global.Instance.GlobalArmaments.HaveExist(this.txtName.Text.Trim()))
                    {
                        MessageBox.Show("名称已经存在");
                        e.Cancel = true;
                        txtName.Focus();
                        return;
                    }
                }

                WriteBackTo((ArmyActionBH)this.treeList.SelectedNodes[0].Tag);
                this.treeList.SelectedNodes[0].Text = ((ArmyActionBH)this.treeList.SelectedNodes[0].Tag).ActionName;

            }
        }

        private void FillInfo(ArmyActionBH armyactionbh)
        {
            comOrg.Value = armyactionbh.Organization;
            txtName.Text = armyactionbh.ActionName;
            txtModelName.Text = armyactionbh.FileName;
            this.maskedTextBox1.Text = armyactionbh.FileScal.ToString();

            numViewHeight.Value = armyactionbh.MaxV;
            numModelViewHeight.Value = armyactionbh.MinV;
        }

        private void WriteBackTo(ArmyActionBH armyactionbh)
        {
            armyactionbh.Organization = (Organizations)comOrg.SelectedItem.DataValue;
            armyactionbh.ActionName = txtName.Text.ToString().Trim();
            armyactionbh.FileName = txtModelName.Text.ToString().Trim();
            armyactionbh.FileScal = Convert.ToDouble(maskedTextBox1.Text) ;
            armyactionbh.MaxV = (double)numViewHeight.Value;
            armyactionbh.MinV = (double)numModelViewHeight.Value;
            Global.Instance.GlobalArmyActionBHs.UpdateData();
        }

        private void comOrg_ValueChanged(object sender, EventArgs e)
        {

        }

        private void treeList_AfterSelect_1(object sender, SelectEventArgs e)
        {
            if (e.NewSelections.Count <= 0)
            {
                return;
            }

            FillInfo((ArmyActionBH)e.NewSelections[0].Tag);
        }

        private void numViewHeight_ValueChanged(object sender, EventArgs e)
        {

        }

        private void txtName_ValueChanged(object sender, EventArgs e)
        {

        }

        private void txtName_KeyUp(object sender, KeyEventArgs e)
        {
            modify = true;
        }

        private void numModelZoom_MouseUp(object sender, MouseEventArgs e)
        {
            modify = true;
        }

        private void numViewHeight_MouseUp(object sender, MouseEventArgs e)
        {
            modify = true;
        }

        private void numModelViewHeight_MouseUp(object sender, MouseEventArgs e)
        {
            modify = true;
        }

        private void FormArmyActionBH_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (modify)
            {
                if (MessageBox.Show("您确定要关闭吗?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }
        }

        private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {

        }

        private void maskedTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //只能输入数字、backspace
            if (!((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8))
            {
                e.Handled = true;
            }
        }

     
    }
}
